<?php
declare (strict_types = 1);

namespace app\cli\controller;

use think\facade\Db;
use think\facade\Env;
use tools\ftp\Ftp;

class Index
{
    public function index()
    {
        return '您好！这是一个[cli]示例应用';
    }

    // 测试邮件发送
    public function sendEmail()
    {
        $transport = (new \Swift_SmtpTransport('smtp.qq.com', 25))
            ->setUsername('2812313382@qq.com')
            ->setPassword('ffckgtybfkordhae');

        $mailer = new \Swift_Mailer($transport);
        $message = (new \Swift_Message('确认注册'))
            ->setFrom(['2812313382@qq.com' => '清博'])
            ->setTo(['1431210468@qq.com'])
            ->setBody('你的验证码为：6666666');

        $result = $mailer->send($message);
        if (empty($result)) {
            return '失败';
        }

        return '成功';
    }

    // 测试ftp文件上传
    public function testFtpUpload()
    {
        $host = Env::get('ftp.host');
        $user = Env::get('ftp.user');
        $password = Env::get('ftp.password');
        $ftp = new Ftp($host, $user, $password);
        $result = $ftp->connect();
        if (!$result) {
            echo $ftp->getErrorMsg();
            exit();
        }
        $localFile = app()->getRootPath() . 'public\\uploads\\1.jpg';
        $remoteFile = date('Y-m-d') . '/1.jpg';
        if ($ftp->upload($localFile, $remoteFile)) {
            echo '上传成功';
        } else {
            echo $ftp->getErrorMsg();
        }
        $ftp->close();
    }

    public function testFtpUpload2()
    {

    }

    // 修改任务相关数据库
    public function taskMove()
    {
        $taskList = Db::table('task')->select()->toArray();
        foreach ($taskList as $v) {
            $data = [
                'task_id' => $v['id'],
                'content' => $v['desc']
            ];
            Db::name('task_content')->save($data);
        }

    }

    // 修改项目相关数据
    public function projectMove()
    {
        $projectList = Db::table('project')->select()->toArray();
        foreach ($projectList as $v) {
            $data = [
                'project_id' => $v['id'],
                'content' => '&lt;p&gt;'.$v['desc']
            ];
            Db::name('project_content')->save($data);
        }
    }

}
